﻿using Application.Abstractions.Messaging;
using Application.Users.Login;
using Mediator;
using SharedKernel;
using WebApi.Extensions;
using WebApi.Infrastructure;

namespace WebApi.Endpoints.Users;

internal sealed class Login : IEndpoint
{
    public sealed record Request(string Email, string Password);

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("users/login", async (
                Request request,
                IMediator mediator,
                CancellationToken cancellationToken) =>
            {
                var command = new LoginUserCommand(request.Email, request.Password);

                Result<string> result = await mediator.Send(command, cancellationToken);
                return result.Match(Results.Ok, ApiResponse.Problem);
            })
            .WithTags(Tags.Users)
            .HasApiVersion(1, 0);
    }
}
